package com.mljr.leida.controller;

import com.alibaba.fastjson.JSON;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author chi.song@mljr.com
 */
@Controller
public class AppController {

    @RequestMapping(value = "/index")
    public String toPage() {
        return "index";
    }

    /**
     * "city_code":"",
     * "brand":"",
     * "model":"",
     * "auto":"",
     * "color":"",
     * "miles_range":[100,10000],
     * "price_range":[1000,100000],
     *
     * @return
     */

    @RequestMapping(value = "/subscribe/new")
    @ResponseBody
    public String leidaNew(@RequestParam(required = false) String city_code,
                           @RequestParam(required = false) String brand,
                           @RequestParam(required = false) String model,
                           @RequestParam(required = false) String auto,
                           @RequestParam(required = false) String color,
                           @RequestParam(required = false) String miles_range,
                           @RequestParam(required = false) String price_range) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("city_code", city_code);
        map.put("brand", brand);
        map.put("model", model);
        map.put("auto", auto);
        map.put("color", color);
        map.put("miles_range", miles_range);
        map.put("price_range", price_range);
        Integer openid = new Random().nextInt(1000000);
        System.out.println("openid is " + openid + "     " + JSON.toJSONString(map));
        try {
            HttpResponse<String> response = Unirest.post("http://10.111.110.27:8081/subscribe/new")
                    .header("openid", openid+"")
                    .body(JSON.toJSONString(map)).asString();
            System.out.println("openid is " + openid + " ------  " + response.getBody());
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return "new";
    }
}
